package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.risetechnology.adapter.CalendarAdapter;
import com.risetechnology.common.Const;
import com.risetechnology.common.Shared;
import com.risetechnology.master.NavigationActivity;

public class ActivityCalendar extends NavigationActivity {

	private static Calendar month;
	private static CalendarAdapter adapter;
	private static Handler handler;
	public static ArrayList<String> items;
	
	private static TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_calendar);

		// Set contents
		setContents();
	}

	@SuppressLint("SimpleDateFormat")
	private void setContents() {
		month = Calendar.getInstance();
		onNewIntent(getIntent());

		items = new ArrayList<String>();
		adapter = new CalendarAdapter(this, month);

		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(adapter);

		handler = new Handler();
		handler.post(calendarUpdater);

		title = (TextView) findViewById(R.id.tv_month);
		title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));

		LinearLayout previous = (LinearLayout) findViewById(R.id.ll_prev);
		previous.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (month.get(Calendar.MONTH) == month
						.getActualMinimum(Calendar.MONTH)) {
					month.set((month.get(Calendar.YEAR) - 1),
							month.getActualMaximum(Calendar.MONTH), 1);
				} else {
					month.set(Calendar.MONTH, month.get(Calendar.MONTH) - 1);
				}
				refreshCalendar();
			}
		});

		LinearLayout next = (LinearLayout) findViewById(R.id.ll_next);
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (month.get(Calendar.MONTH) == month
						.getActualMaximum(Calendar.MONTH)) {
					month.set((month.get(Calendar.YEAR) + 1),
							month.getActualMinimum(Calendar.MONTH), 1);
				} else {
					month.set(Calendar.MONTH, month.get(Calendar.MONTH) + 1);
				}
				refreshCalendar();

			}
		});

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				TextView date = (TextView) v.findViewById(R.id.date);
				if (date instanceof TextView && !date.getText().equals("")) {
					String dayOfWeek = getResources().getStringArray(R.array.week_days)[position % 7];
					String strDayOfMonth = date.getText().toString();
					String month = "";
					String year = "";
					
					TextView title = (TextView) findViewById(R.id.tv_month);
					String monthYear = title.getText().toString();
					String[] monthYearArr = monthYear.split(" ");
					if (monthYearArr != null) {
						month = monthYearArr[0].substring(0, 3);
						year = monthYearArr[1];
					}
					
					Intent intent = new Intent(ActivityCalendar.this.getParent(), ActivityDaySchedule.class);
					intent.putExtra(Const.KEY_DAY_OF_WEEK, dayOfWeek);
					intent.putExtra(Const.KEY_DAY_OF_MONTH, strDayOfMonth);
					intent.putExtra(Const.KEY_MONTH, month);
					intent.putExtra(Const.KEY_YEAR, year);
					
					goNextHistory("Day Schedule", intent);
				}

			}
		});
	}

	public static void refreshCalendar() {
		adapter.refreshDays();
		adapter.notifyDataSetChanged();
		handler.post(calendarUpdater); // generate some random calendar items

		title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
	}

	public void onNewIntent(Intent intent) {
		month = Calendar.getInstance();
	
		TextView title = (TextView) findViewById(R.id.tv_month);
		title.setTypeface(Shared.font);
		title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
	}

	private static Runnable calendarUpdater = new Runnable() {

		@Override
		public void run() {
//			items.clear();
			// format random values. You can implement a dedicated class to
			// provide real values
//			for (int i = 0; i < 31; i++) {
//				Random r = new Random();
//
//				if (r.nextInt(10) > 6) {
//					items.add(Integer.toString(i));
//				}
//			}
			Shared.dbAdapter.getEventDates(month.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US));
			
			adapter.setItems(items);
			adapter.notifyDataSetChanged();
		}
	};
}
